package com.example.takeoff.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.takeoff.common.Result;
import com.example.takeoff.entity.Check;
import com.example.takeoff.mapper.CheckMapper;
import com.example.takeoff.service.CheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class CheckServiceImpl extends ServiceImpl<CheckMapper, Check> implements CheckService
{
    @Autowired
    private CheckMapper checkMapper;

    @Override
    public Result createCode(String phone, String code)
    {
        Check check = new Check();
        check.setPhoneNum(phone);
        check.setCode(code);
        checkMapper.insert(check);
        return Result.success();
    }

    @Override
    public Result checkCode(String phone, String code)
    {
        LambdaQueryWrapper<Check> qw = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Check> eq = qw.eq(Check::getPhoneNum, phone).eq(Check::getCode, code);
        Check check = checkMapper.selectOne(eq);
        if (check == null)
            return Result.error("验证码错误");
        long createdTime = check.getCreateTime().getTime();
        long currentTime = new Date().getTime();
        if(currentTime-createdTime>300000)
            return Result.error("验证码超时");
        return Result.success();
    }


}
